/********************************************************************************
 *  
 *  Product: GSAOBKeyMatchesLib
 *  Description: A C# API for Responsing to Google Search Appliance OneBoxModule.
 *
 *  (c) Copyright 2007 
 *  
********************************************************************************/
using System;
using System.Collections;
using System.Text;

namespace GSAOBKeyMatchesLib.Server
{
    /// <summary>
    /// Provides methods to create OneBoxResults  objects
    /// <para>Author Albert Ghukasyan</para>
    /// </summary>
    class OneBoxResults : IOneBoxResults
    {
        #region Variables

        private String m_resultCode = "success";
        private String m_diagnostics = null;
        private String m_urlText = null;
        private String m_urlLink = null;
        private String m_providerText = null;
        private String m_imageUrl = null;
        private ArrayList m_results = new ArrayList();

        #endregion

        #region Methods

        public void setFailure(FailureCodes failureCode, String diagnosticMessage)
        {
            m_resultCode = failureCode.ToString();
            m_diagnostics = diagnosticMessage;
        }
        public void setProviderText(String providerText)
        {
            m_providerText = providerText;
        }

        public void setResultsTitleLink(String text, String url)
        {
            m_urlText = text;
            m_urlLink = url;
        }
    
        public void setImageUrl(String imageUrl)
        { 
            m_imageUrl = imageUrl; 
        }

        public void addResult(ModuleResult result)
        {
            if (m_results.Count >= 8)
                throw new IndexOutOfRangeException("Attempt to return too many OneBox results");
            m_results.Add(result);
        }
       
        public bool canAddResult()
        {
            return m_results.Count < 8;
        }

        #endregion

        #region Constructor

        public OneBoxResults()
        {

        }

        #endregion

        #region IOneBoxResults Members

        public string Serialize()
        {
            StringBuilder buf = new StringBuilder();
            buf.Append("<?xml version=\"1.0\" encoding=\"UTF-8\"?>");
            buf.Append("<OneBoxResults xmlns:xsi=\"http://www.w3.org/2001/XMLSchema-instance\"")
                .Append(" xsi:noNamespaceSchemaLocation=\"oneboxresults.xsd\">");
            buf.Append("<resultCode>").Append(m_resultCode).Append("</resultCode>");
            if (m_diagnostics != null)
            {
                buf.Append("<Diagnostics>")
                    .Append(m_diagnostics.Substring(0, Math.Min(m_diagnostics.Length, 256)))
                    .Append("</Diagnostics>");
            }
            if (m_providerText != null)
            {
                buf.Append("<provider>")
                    .Append(m_providerText.Substring(0, Math.Min(m_providerText.Length, 128)))
                    .Append("</provider>");
            }
            if ((m_urlText != null) && (m_urlLink != null))
            {
                buf.Append("<title>");
                buf.Append("<urlText>")
                    .Append(m_urlText.Substring(0, Math.Min(m_urlText.Length, 40)))
                    .Append("</urlText>");
                buf.Append("<urlLink>").Append(m_urlLink).Append("</urlLink>");
                buf.Append("</title>");
            }
            if (m_imageUrl != null)
            {
                buf.Append("<IMAGE_SOURCE>").Append(m_imageUrl).Append("</IMAGE_SOURCE>");
            }
            foreach (ModuleResult moduleResult in m_results)
            {
                buf.Append("<MODULE_RESULT>");
                String url = moduleResult.getUrl();
                if (url != null)
                {
                    buf.Append("<U>").Append(url).Append("</U>");
                }
                String text = moduleResult.getText();
                if (text != null)
                {
                    buf.Append("<Title>").Append(text).Append("</Title>");
                }
                foreach(FieldObject field in moduleResult.getFields())
                {
                    buf.Append("<Field");
                    foreach (String attr in field.getAttrs())
                    {
                        buf.Append(" ").Append(attr);
                    }
                    buf.Append(">");
                    if (field.getValue() != null)
                    {
                        buf.Append(field.getValue());
                    }
                    buf.Append("</Field>");
                }
                buf.Append("</MODULE_RESULT>");
            }
            buf.Append("</OneBoxResults>");
            return buf.ToString();
        }

        #endregion
    }
}
